clear all
set more off
tempfile temp xxx

** PATHS
local ubigeo ../../ubigeo

** DATA
use `ubigeo'/ubigeo1993

sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

drop if coddist == "00"
gen ubigeo = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

gen y = _n
rename ubigeo ubigeo1993
save `temp', replace


use `ubigeo'/ubigeo2010, clear 
sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

drop if coddist == "00"
gen ubigeo2010 = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

keep ubigeo *name*
gen x = _n

reclink DEPTname PROVname DISTname using `temp', idmaster(x) idusing(y) gen(score)
tab _merge
destring ubigeo*, replace
gen ubigeohost = ubigeo2010
gsort -score ubigeohost
gen dd = DISTname == UDISTname
tab ubigeohost if _merge == 1

replace ubigeo1993 = 010201 if ubigeohost == 010201 & _merge == 1 // bagua to la peca
replace ubigeo1993 = 021801 if ubigeohost == 021809 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030202 if ubigeohost == 030219 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030501 if ubigeohost == 030506 & _merge == 1 // challhuahuacho to tambobamba
replace ubigeo1993 = 030706 if ubigeohost == 030714 & _merge == 1 // curasco to micaela bastidas
replace ubigeo1993 = 040112 if ubigeohost == 040129 & _merge == 1 // Jose L Bust. y Rivero to paucarpata
replace ubigeo1993 = 040511 if ubigeohost == 040520 & _merge == 1 // majes to lluta
replace ubigeo1993 = 050101 if ubigeohost == 050115 & _merge == 1 // jesus nazareno to ayacucho
replace ubigeo1993 = 050407 if ubigeohost == 050408 & _merge == 1 // llochegua to sivia
replace ubigeo1993 = 050501 if ubigeohost == 050509 & _merge == 1 // samugari to san miguel
replace ubigeo1993 = 060307 if ubigeohost == 060312 & _merge == 1 // la libertad de pallan to miguel iglesias
replace ubigeo1993 = 060413 if ubigeohost == 060419 & _merge == 1 // chalamarca to paccha
replace ubigeo1993 = 080806 if ubigeohost == 080808 & _merge == 1 // alto pichigua to pichigua
replace ubigeo1993 = 090101 if ubigeohost == 090118 & _merge == 1 // ascension to huancavelica
replace ubigeo1993 = 090502 if ubigeohost == 090511 & _merge == 1 // cosme to anco
replace ubigeo1993 = 100302 if ubigeohost == 101104 & _merge == 1 // aparicio pomares to chupa
replace ubigeo1993 = 100306 if ubigeohost == 101108 & _merge == 1 // choras to chavinillo
replace ubigeo1993 = 130201 if ubigeohost == 130208 & _merge == 1 // casa grande to ascope
replace ubigeo1993 = 130112 if ubigeohost == 131202 & _merge == 1 // cguadalupito to old viru
replace ubigeo1993 = 130112 if ubigeohost == 131203 & _merge == 1 // viru to old viru
replace ubigeo1993 = 140115 if ubigeohost == 140116 & _merge == 1 // patapo to sana
replace ubigeo1993 = 140115 if ubigeohost == 140117 & _merge == 1 // pucala to sana
replace ubigeo1993 = 140115 if ubigeohost == 140119 & _merge == 1 // cayalti to sana
replace ubigeo1993 = 140101 if ubigeohost == 140118 & _merge == 1 // pomalca to cix
replace ubigeo1993 = 140101 if ubigeohost == 140120 & _merge == 1 // tuman to cix
replace ubigeo1993 = 160401 if ubigeohost == 160404 & _merge == 1 // san pablo to ramon castill
replace ubigeo1993 = 160507 if ubigeohost == 160510 & _merge == 1 // jenaro herrera to saquena
replace ubigeo1993 = 160209 if ubigeohost == 160706 & _merge == 1  // andoas to pastaza
replace ubigeo1993 = 170101 if ubigeohost == 170104 & _merge == 1 // laberinto to tambopata
replace ubigeo1993 = 170201 if ubigeohost == 170204 & _merge == 1 // huetetopue to manu
replace ubigeo1993 = 190306 if ubigeohost == 190308 & _merge == 1 // huetetopue to manu
replace ubigeo1993 = 211201 if ubigeohost == 211209 & _merge == 1 // alto inambaro to sandia
replace ubigeo1993 = 211207 if ubigeohost == 211210 & _merge == 1 // putina to san juan
replace ubigeo1993 = 230101 if ubigeohost == 230110 & _merge == 1 // albarracin to tacna
replace ubigeo1993 = 240201 if ubigeohost == 240203 & _merge == 1 // albarracin to tacna
replace ubigeo1993 = 250301 if ubigeohost == 250303 & _merge == 1 // curimana to p abad


tab ubigeohost if ubigeo1993 == . & _merge == 1

gen flag = (score < 0.7 & _merge == 3 & dd == 0)
tab ubigeohost if flag == 1

replace ubigeo1993 = 030601 if ubigeohost == 030608 & flag == 1 // curimana to p abad
replace flag = 0 if ubigeohost == 030608 & flag == 1

replace ubigeo1993 = 080907 if ubigeohost == 080910 & flag == 1 // pichari to kimbiri
replace flag = 0 if ubigeohost == 080910 & flag == 1

replace ubigeo1993 = 100102 if ubigeohost == 100111 & flag == 1 // pillco arca to amarilis
replace flag = 0 if ubigeohost == 100111 & flag == 1

replace ubigeo1993 = 100106 if ubigeohost == 100112 & flag == 1 // yauco to quisqui
replace flag = 0 if ubigeohost == 100112 & flag == 1

replace flag = 0 if ubigeohost == 120908 & flag == 1 // tres de diciembre
replace flag = 0 if ubigeohost == 150121 & flag == 1 // pueblo libre to magdalena vieja

replace ubigeo1993 = 160101 if ubigeohost == 160112 & flag == 1  // belen to iquitos
replace flag = 0 if ubigeohost == 160112 & flag == 1

replace ubigeo1993 = 160101 if ubigeohost == 160113 & flag == 1 // san juan bautista to iquitos
replace flag = 0 if ubigeohost == 160113 & flag == 1

replace ubigeo1993 = 160109 if ubigeohost == 160114 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 160114 & flag == 1

replace ubigeo1993 = 210504 if ubigeohost == 210505 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 210505 & flag == 1

replace flag = 0 if ubigeohost == 230402 & flag == 1 // albarracin to chucatami

replace ubigeo1993 = 250101 if ubigeohost == 250107 & flag == 1 // manantay to calleria
replace flag = 0 if ubigeohost == 250107 & flag == 1

replace ubigeo1993 = 250102 if ubigeohost == 250106 & flag == 1 // n requena to campo verde
replace flag = 0 if ubigeohost == 250106 & flag == 1

tab ubigeohost if flag == 1


keep ubigeo2010 ubigeo1993
duplicates drop

save `ubigeo'/ubigeo2010to1993, replace

e








replace ubigeo1993 = 030605 if ubigeohost == 030608 & _merge == 1 // ranracancha to ocobamba
replace ubigeo1993 = 100102 if ubigeohost == 100111 & _merge == 1 // pillco arca to amarilis
replace ubigeo1993 = 100310 if ubigeohost == 101002 & _merge == 1 // banos to jivia
replace ubigeo1993 = 130609 if ubigeohost == 131103 // cormin
replace ubigeo1993 = 160111 if ubigeohost == 160511 // yaqyerana
replace ubigeo1993 = 210504 if ubigeohost == 210505 & _merge == 1 // conduriri santa rosa

replace ubigeo1993 = 250102 if ubigeohost == 250106 & _merge == 1 // n requena to campo verde
replace ubigeo1993 = 140115 if ubigeohost == 140116 // patapo to sana
replace ubigeo1993 = 140115 if ubigeohost == 140117 // pucala to sana
replace ubigeo1993 = 140115 if ubigeohost == 140119 // cayalti to sana
replace ubigeo1993 = 140101 if ubigeohost == 140118 // pomalca to cix
replace ubigeo1993 = 140101 if ubigeohost == 140120 // tuman to cix

duplicates report ubigeo1993
tab ubigeohost if ubigeo1993 == . & _merge == 1
e
keep ubigeo2008 ubigeo1993 
duplicates drop

** SAVE
save `ubigeo'/ubigeo2008to1993, replace













e
sort ubigeo2008
gen u2008to1993 = ubigeo2008
order ubigeo2008 u2008 ubigeo1993 fecha

replace u2008to1993 = "010201" if u2008to1993 == "010206" // bagua to la peca
replace u2008to1993 = "021801" if u2008to1993 == "021809" // nuevo chimbote to old chimbote
replace u2008to1993 = "030202" if u2008to1993 == "030219" // nuevo chimbote to old chimbote
replace u2008to1993 = "030501" if u2008to1993 == "030506" // challhuahuacho to tambobamba
replace u2008to1993 = "030605" if u2008to1993 == "030608" // ranracancha to ocobamba
replace u2008to1993 = "030706" if u2008to1993 == "030714" // curasco to micaela bastidas
replace u2008to1993 = "040112" if u2008to1993 == "040129" // Jose L Bust. y Rivero to paucarpata

* unite many districts around majes and call them lluta (under its ubigeo)
replace u2008to1993 = "040511" if u2008to1993 == "040520" // majes to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040120" // sta i de sihuas to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040118" // s j de sihuas to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040413" // uraca to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040407" // huancarqui to lluta

replace u2008to1993 = "050101" if u2008to1993 == "050115" // jesus nazareno to ayacucho
replace u2008to1993 = "050407" if u2008to1993 == "050408" // llochegua to sivia
replace u2008to1993 = "060307" if u2008to1993 == "060312" // la libertad de pallan to miguel iglesias
replace u2008to1993 = "060413" if u2008to1993 == "060419" // chalamarca to paccha
replace u2008to1993 = "080806" if u2008to1993 == "080808" // alto pichigua to pichigua
replace u2008to1993 = "080907" if u2008to1993 == "080910" // pichari to kimbiri
replace u2008to1993 = "090101" if u2008to1993 == "090118" // ascension to huancavelica
replace u2008to1993 = "100102" if u2008to1993 == "100111" // pillco arca to amarilis
replace u2008to1993 = "100306" if u2008to1993 == "101108" // choras to chavinillo
replace u2008to1993 = "100302" if u2008to1993 == "101104" // aparicio pomares to chupa
replace u2008to1993 = "130201" if u2008to1993 == "130208" // casa grande to ascope
replace u2008to1993 = "130112" if u2008to1993 == "131201" // chao to old viru
replace u2008to1993 = "130112" if u2008to1993 == "131202" // cguadalupito to old viru
replace u2008to1993 = "130112" if u2008to1993 == "131203" // viru to old viru
replace u2008to1993 = "130609" if u2008to1993 == "131103" // cormin
replace u2008to1993 = "140115" if u2008to1993 == "140116" // patapo to sana
replace u2008to1993 = "140115" if u2008to1993 == "140117" // pucala to sana
replace u2008to1993 = "140115" if u2008to1993 == "140119" // cayalti to sana
replace u2008to1993 = "140101" if u2008to1993 == "140118" // pomalca to cix
replace u2008to1993 = "140101" if u2008to1993 == "140120" // tuman to cix
replace u2008to1993 = "160101" if u2008to1993 == "160112" // belen to iquitos
replace u2008to1993 = "160101" if u2008to1993 == "160113" // san juan bautista to iquitos
replace u2008to1993 = "160109" if u2008to1993 == "160114" // tentiente.. to putumayo
replace u2008to1993 = "160401" if u2008to1993 == "160404" // san pablo to ramon castill
replace u2008to1993 = "160111" if u2008to1993 == "160511" // yaqyerana
replace u2008to1993 = "160507" if u2008to1993 == "160510" // jenaro herrera to saquena
replace u2008to1993 = "160201" if u2008to1993 == "160706" // andoas
replace u2008to1993 = "170101" if u2008to1993 == "170104" // laberinto to tambopata
replace u2008to1993 = "170201" if u2008to1993 == "170204" // manu to madre de dios
replace u2008to1993 = "210504" if u2008to1993 == "210505" // conduriri santa rosa
replace u2008to1993 = "211201" if u2008to1993 == "211209" // alto inambaro to sandia
replace u2008to1993 = "211207" if u2008to1993 == "211210" // putina to san juan
replace u2008to1993 = "230101" if u2008to1993 == "230110" // albarracin to tacna
replace u2008to1993 = "240201" if u2008to1993 == "240203" // albarracin to tacna
replace u2008to1993 = "250101" if u2008to1993 == "250107" // manantay to calleria
replace u2008to1993 = "250102" if u2008to1993 == "250106" // n requena to campo verde
replace u2008to1993 = "250301" if u2008to1993 == "250303" // curimana to p abad

replace ubigeo1993 = u2008to1993 if new == 1
replace ubigeo1993 = u2008to1993 if ubigeo1993 == ""

drop _merge
merge m:1 ubigeo1993 using `temp'
keep if _merge == 3

keep ubigeo2008 ubigeo1993

** SAVE
rename ubigeo2008 ubigeo1995
save `working'/ubigeo1995to1993, replace
